<?php

class My_Html_List
{
    /**
     * @var string
     */
    protected $id;
    
    /**
     * @var string 
     */
    protected $title;
    /**
     * @var boolean
     */
    protected $showFilter = false;
    /**
     * @var boolean
     */
    protected $showTitle = true;
    /**
     * @var boolean
     */
    protected $showOrderer = false;
    /**
     * @var array
     */
    protected $data = array();
    /**
     * @var string
     */
    protected $viewColumnKey = '0';
    /**
     * @var string
     */
    protected $filterColumnKey = '0';
    
    /**
     * @var array
     */
    protected $columnKeys;
    /**
     * @var integer
     */
    protected $itemsPerPage = 20;
    /**
     * @var string
     */
    protected $dataUrl;
    /**
     * @var string
     */
    protected $renderScript = 'default.phtml';
    
    protected function __construct($id)
    {
        $this->id = $id;
        $this->view = new Zend_View();
        $this->view->addScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'list');
        $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
    }
    
    /**
     * @return My_Html_List
     */
    public static function factory($id = null)
    {
        if (is_null($id)) $id = uniqid();
        return new self($id);
    } 
    
    /**
     * @return boolean
     */
    public function getShowFilter()
    {
        return $this->showFilter;
    }


    public function setShowFilter($showFilter)
    {
        $this->showFilter = $showFilter;
        return $this;
    }

    /**
     * @return boolean
     */
    public function getShowTitle()
    {
        return $this->showTitle;
    }

    /**
     * @param boolean $showTitle
     * @return My_Html_List
     */
    public function setShowTitle($showTitle)
    {
        $this->showTitle = $showTitle;
        return $this;
    }

    /**
     * @return boolean
     */
    public function getShowOrderer()
    {
        return $this->showOrderer;
    }

    /**
     * @param boolean $showOrderer
     * @return My_Html_List
     */
    public function setShowOrderer($showOrderer)
    {
        $this->showOrderer = $showOrderer;
        return $this;
    }
    
    /**
     * @return string
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $id
     * @return My_Html_List
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @param string $title
     * @return My_Html_List
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }

    /**
     * @return integer
     */
    public function getItemsPerPage()
    {
        return $this->itemsPerPage;
    }

    /**
     * @param integer $intemsPerPage
     * @return My_Html_List
     */
    public function setItemsPerPage($itemsPerPage)
    {
        $this->itemsPerPage = $itemsPerPage;
        return $this;
    }

    /**
     * @return integer
     */
    public function getNumberOfPages()
    {
        $num = ceil(count($this->getData()) / $this->getItemsPerPage());
        if ($num <= 0) $num = 1;
        return $num;
    }    
    /**
     * @return array
     */
    public function getData($jsonEncoded = false)
    {
        $data = $this->data;
        if (true == $jsonEncoded)
        {
            $data = Zend_Json::encode($data);
        }    
        return $data;
    }

    /**
     * @param array $data
     * @return My_Html_List
     */
    public function setData($data)
    {
        $this->data = $data;
        return $this;
    }
    
    /**
     * @param mixed $data
     * @return My_Html_List
     */
    public function addData($data)
    {
        if (!is_array($data)) $data = array($data);
        $this->data[] = $data;
        return $this;
    }  
    
    /**
     * @return array
     */
    public function getColumnKeys()
    {
        if (is_null($this->columnKeys))
        {
            $this->columnKeys = array();
            $data = $this->getData();
            if (is_array($data) && count($data)>0)
            {
                $this->columnKeys = array_keys(current($data));
            }
        }  
        return $this->columnKeys;
    }    
    
    /**
     * @return string
     */
    public function getDataUrl()
    {
        return $this->dataUrl;
    }

    /**
     * @param string $dataUrl
     * @return My_Html_List
     */
    public function setDataUrl($dataUrl)
    {
        $this->dataUrl = $dataUrl;
        return $this;
    }

        
    /**
     * @return string Json encoded array
     */
    public function getJsConfiguration()
    {
        $config = array();
        $config['itemsPerPage'] = $this->getItemsPerPage();
        $config['columnKeys'] = $this->getColumnKeys();
        $config['viewColumnKey'] = $this->getViewColumnKey();
        $config['filterColumnKey'] = $this->getFilterColumnKey();
        $config['showFilter'] = $this->getShowFilter();
        $config['showOrderer'] = $this->getShowOrderer();
        if (!is_null($this->getDataUrl()))
        {
            $config['dataUrl'] = $this->getDataUrl();            
        }    
        return Zend_Json::encode($config);
    }    

    /**
     * @return boolean
     */
    public function getViewColumnKey()
    {
        return $this->viewColumnKey;
    }

    /**
     * @param string $viewColumn
     * @return My_Html_List
     */
    public function setViewColumnKey($viewColumnKey)
    {
        $this->viewColumnKey = $viewColumnKey;
        return $this;
    }

    /**
     * @return string
     */
    public function getFilterColumnKey()
    {
        return $this->filterColumnKey;
    }

    /**
     * @param string $filterColumnKey
     * @return My_Html_List
     */
    public function setFilterColumnKey($filterColumnKey)
    {
        $this->filterColumnKey = $filterColumnKey;
        return $this;
    }

        /**
     * @return Zend_View
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param Zend_View $view
     * @return My_Html_List
     */
    public function setView($view)
    {
        $this->view = $view;
        return $this;
    }

    /**
     * @return string
     */
    public function getRenderScript()
    {
        $renderScript = $this->renderScript;
        return $renderScript;
    }

    /**
     * @param string $renderScript
     * @return My_Html_List
     */
    public function setRenderScript($renderScript)
    {
        $this->renderScript = $renderScript;
        return $this;
    }

    /**
     * @return string
     */
    public function render()
    {
        $view = $this->getView();
        $view->list = $this;
        return $view->render($this->getRenderScript());
    }

    /**
     * @return string
     */
    public function __toString()
    {
        try
        {
            $htmlCode = $this->render();
        }
        catch (Exception $e)
        {
            $htmlCode = sprintf('Error rendering list %s: %s (%s).', $this->getId(), $this->getTitle(), $e->getMessage());
        }
        return $htmlCode;
    }

    
}